package chapter10.exercise.test02;

public class Sequence {
    private Object[] items;
    private int next = 0;
    private String label;

    public Sequence(int size, String label) {
        this.items = new Object[size];
        this.label = label;
    }

    public void add(Object obj) {
        if (next < items.length) {
            items[next++] = obj;
        }
    }

    private class SequenceSelector implements Selector {
        private int i = 0;

        @Override
        public boolean end() {
            return i == items.length;
        }

        @Override
        public Object current() {
            return items[i];
        }

        @Override
        public void next() {
            if (i < items.length) {
                i++;
            }
        }

        public void read() {
            System.out.println(label);
        }

        public Sequence getSequence() {
            return Sequence.this;
        }
    }

    private class Read {
        public void read() {
            System.out.println(label);
        }
    }

    public Read getRead() {
        return new Read();
    }

    public Selector getSelector() {
        return new SequenceSelector();
    }

    public static void main(String[] args) {
        Sequence se = new Sequence(10, "你");
        for (int i = 0; i < 10; i++) {
            se.add(i + 1);
        }
        Sequence.SequenceSelector s = se.new SequenceSelector();
        Sequence sequence = s.getSequence();
        sequence.getRead().read();
    }
}
